﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Model;
using NHibernate;
using Senparc.Weixin.MP.Entities;
using NHibernate.Criterion;
using System.Collections.Generic;
using ZLCMS.Business.Util;
using Senparc.Weixin.MP.Helpers;
using DotNet.Common.NHibernateUtil;
using Newtonsoft.Json;
using ZLCMS.Business.WeixinService.Data;

namespace ZLCMS.Web.SystemManage.Weixin.Interaction.SceneDisplay
{
    public partial class SceneEdit : System.Web.UI.Page
    {
        protected FrameHelper _frameHelper;

        protected WxGamesConfig _gameDb;
        protected WxMaterial _materialDb;

        private string _uid;

        protected string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            _frameHelper = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();
            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                Save();
            }
        }

        #region 保存操作 新建/更新 + private void Save()
        /// <summary>
        /// 保存操作 新建/更新
        /// </summary>
        private void Save()
        {
           
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ITransaction tran = session.BeginTransaction();
                try
                {
                    if (string.IsNullOrEmpty(_uid))//新建
                    {
                        _materialDb = new WxMaterial();

                        _materialDb.Title = tb_name.Text;
                        _materialDb.MsgType = "Scene";
                        _materialDb.CreateDate = DateTime.Now;
                        _materialDb.UpdateDate = DateTime.Now;
                        _materialDb.Status = "1";
                        _materialDb.OwnerAccount = _userAccount;

                        session.Save(_materialDb);

                        _gameDb = new WxGamesConfig();

                        _gameDb.Title = tb_name.Text;
                        _gameDb.Description = "场景配置";
                        _gameDb.Content = "";

                        _gameDb.BeginDate = DateTime.Now;
                        _gameDb.EndDate = DateTime.Now.AddDays(7);
                        _gameDb.CreateDate = DateTime.Now;

                        _gameDb.ExConfig = fc_imgurl.Value;
                        _gameDb.GameBackgroupUrl = null;

                        _gameDb.NeedConfig =  0;
                        _gameDb.EndNotice = "";

                        _gameDb.OwnerAccount = _userAccount;
                        _gameDb.GameType = "Scene";
                        _gameDb.Status = "RUNNING";
                        _gameDb.MaterialId = _materialDb.Id;

                        session.Save(_gameDb);
                    }
                    else//修改
                    {
                        _materialDb.Title = tb_name.Text;

                        _gameDb.Title = tb_name.Text;
                        _gameDb.Description = "场景配置";
                        _gameDb.BeginDate = DateTime.Now;
                        _gameDb.EndDate = DateTime.Now.AddDays(7);

                        //活动开始后，不能修改奖品数量
                        _gameDb.ExConfig = fc_imgurl.Value;
                        
                        _gameDb.NeedConfig = 0;
                        _gameDb.EndNotice = "";

                        session.Update(_materialDb);
                        session.Update(_gameDb);
                    }

                    session.Flush();
                    tran.Commit();
                    //session.Flush();
                }
                catch (System.Exception ex)
                {
                    tran.Rollback();
                    _frameHelper.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }

            _frameHelper.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                navTabId = _frameHelper.NavTabId
            });
        } 
        #endregion

        protected void CreateAward(ISession session, WxGamesConfig gameDb)
        {

        }

        #region 初始化全局变量 + protected void InitializeGlobalValue()
        /// <summary>
        /// 初始化全局变量
        /// </summary>
        protected void InitializeGlobalValue()
        {
            if (!IsPostBack)
            {
                ViewState["uid"] = Request["uid"];
            }
            _uid = (string)ViewState["uid"];

            if (!string.IsNullOrEmpty(_uid))
            {
                using (ISession session = SessionFactoryHelper.CreateSession())
                {
                    try
                    {
                        _gameDb = session.CreateCriteria<WxGamesConfig>().Add(Expression.Eq("Id", int.Parse(_uid))).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxGamesConfig>();

                        if (_gameDb == null)
                        {
                            throw new Exception("找不到对应的游戏配置记录.");
                        }
                        else
                        {
                            _materialDb = session.CreateCriteria<WxMaterial>().Add(Expression.Eq("Id", _gameDb.MaterialId)).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxMaterial>();
                            if (_materialDb == null)
                            {
                                throw new Exception("找不到对应的素材记录");
                            }
                        }
                    }
                    catch (System.Exception ex)
                    {
                        _frameHelper.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                        return;
                    }
                }
            }
        } 
        #endregion

        #region 填充页面内容 + protected void InitializeContent()
        /// <summary>
        /// 填充页面内容
        /// </summary>
        protected void InitializeContent()
        {
            //页面控件值
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(_uid))
                {
                    tb_name.Text = _gameDb.Title;
                    fc_imgurl.Value = _gameDb.ExConfig;

                }
                else
                {
                    tb_name.Text = "场景配置";
                }
            }
        } 
        #endregion
    }
}
